package com.example.flowable.config;

import javax.sql.DataSource;
import org.flowable.common.engine.impl.AbstractEngineConfiguration;
import org.flowable.common.engine.impl.EngineConfigurator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.stereotype.Component;

@Component
public class DatasourceConfigurator implements EngineConfigurator {
    @Value("${datasource.flowable.jdbc-url}")
    private String url;

    @Value("${datasource.flowable.type}")
    private Class<? extends DataSource> type;

    @Value("${datasource.flowable.driver-class-name}")
    private String driverClassName;

    @Value("${datasource.flowable.username}")
    private String username;

    @Value("${datasource.flowable.password}")
    private String password;
    @Override
    public void beforeInit(AbstractEngineConfiguration abstractEngineConfiguration) {
        // DataSource dataSource = FlowableDataSource.getDataSource();
        DataSource dataSource = DataSourceBuilder.create()
                .type(type)
                .driverClassName(driverClassName)
                .url(url)
                .username(username)
                .password(password).build();
        abstractEngineConfiguration.setDataSource(dataSource);
    }

    @Override
    public void configure(AbstractEngineConfiguration abstractEngineConfiguration) {

    }

    @Override
    public int getPriority() {
        return 600000;
    }
}
